package com.tma.timesheettracker.contentprovider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TimeTrackerDatabase extends SQLiteOpenHelper {

	private static final String DB_NAME = "TimeTrackerDatabase";
	private static final int DB_Version = 1;
	public static final String TABLE_USER = "user";
	public static final String TABLE_TASK = "task";
	public static final String TABLE_TRACKING_ENTRY = "trackingentry";

	private static final String CREATE_USER = "create table " + TABLE_USER
			+ " (" + TimeTrackerContentProvider.UserTable.USERID
			+ " integer primary key autoincrement, "
			+ TimeTrackerContentProvider.UserTable.USERNAME
			+ " TEXT NOT NULL, "
			+ TimeTrackerContentProvider.UserTable.PASSWORD
			+ " TEXT NOT NULL);";
	private static final String CREATE_TRACKINGENTRY = "create table "
			+ TABLE_TRACKING_ENTRY + " ("
			+ TimeTrackerContentProvider.TrackingEntryTable.ID
			+ " integer primary key autoincrement, "
			+ TimeTrackerContentProvider.TrackingEntryTable.USERID
			+ " integer NOT NULL, "
			+ TimeTrackerContentProvider.TrackingEntryTable.TASK_ID
			+ " integer , "
			+ TimeTrackerContentProvider.TrackingEntryTable.DURATION
			+ " integer NOT NULL, "
			+ TimeTrackerContentProvider.TrackingEntryTable.DATE
			+ " DATETIME NOT NULL);";

	private static final String CREATE_TASK = "create table " + TABLE_TASK
			+ " (" + TimeTrackerContentProvider.TaskTable.TASKID
			+ " integer primary key autoincrement, "
			+ TimeTrackerContentProvider.TaskTable.TASKNAME
			+ " TEXT NOT NULL);";

	public TimeTrackerDatabase(Context context) {
		super(context, DB_NAME, null, DB_Version);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_USER);
		db.execSQL(CREATE_TASK);
		db.execSQL(CREATE_TRACKINGENTRY);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRACKING_ENTRY);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASK);
		onCreate(db);
	}

}
